import 'package:flutter/material.dart';
import 'package:we_chat/pages/discover/discover_child_page.dart';

class DiscoverCell extends StatefulWidget {
  final String title;
  final String? subTitle;//没加required，必须加？表示可空
  final String imageName;
  final String? subImageName;

  //构造函数
  DiscoverCell({
    // Key? key,
    required this.title,//不加？必须加required表示不可空
    this.subTitle,
    required this.imageName,
    this.subImageName
  }) : assert(title != null, 'title 不能为空'),
        assert(imageName != null, 'imageName 不能为空');

  @override
  State<DiscoverCell> createState() => _DiscoverCellState();
}

class _DiscoverCellState extends State<DiscoverCell> {

  Color _currentColor = Colors.white;

  @override
  Widget build(BuildContext context) {
    return GestureDetector(//增加手势
      onTap: (){
        Navigator.of(context).push(
          //只有一行用箭头行数
          MaterialPageRoute(builder: (BuildContext context) => DiscoverChildPage(title: widget.title),),
        );
        setState((){//
          _currentColor = Colors.white;//修改点击状态
        });
      },
      onTapDown: (TapDownDetails details){
        setState((){
          _currentColor = Colors.grey;//修改点击状态
        });
      },
      child: Container(
        color:_currentColor,
        height: 54,
        child: Row(//横行布局
          mainAxisAlignment: MainAxisAlignment.spaceBetween,//左右两边靠
          children: [
            //left
            Container(
              padding: EdgeInsets.all(10.0),
              child: Row(
                children: [
                  Image(image: AssetImage(widget.imageName),width: 20,),
                  SizedBox(width: 15,),//分开
                  Text(widget.title),
                ],
              ),
            ),

            //right
            Container(
              padding: EdgeInsets.all(10),
              child: Row(children: [
                widget.subTitle != null?Text(widget.subTitle!):Text(''),//可空使用的时候，要加！
                widget.subImageName != null?Image(image:AssetImage(widget.subImageName!),width: 12,):Container(),
                Image(image: AssetImage('images/icon_right.png'),width: 15,)//Image.asset无法设置大小
              ],),
            )
          ],
        ),
      ),
    );
  }
}
